﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blog.Helper;
using Blog.Models;
using Blog.Models.CommentServices;
using Blog.Models.MenuServices;
using Blog.Models.UserServices;
using Spirit.Portal.Core.Web;

namespace Blog.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/

        private IMenuRepository _re;
        public MenuController()
        {
            _re=new MenuRepository();
        }
        public MenuController(IMenuRepository re)
        {
            _re = re;
        }
        public IUserRepository re=new UserRepository();
        private ICommentRepository _re1 = new CommentRepository();
        private IArticleCommentRepository _re2 = new ArticleCommentRepository();

        public ActionResult Index(int? page,int id)
        {
            var item = _re.FindAlldata(id);
            const int PageSize = 4;
            var ds = new PagetedList<Article>(item, page ?? 0, PageSize);
            return View(ds);
        }

        //
        // GET: /Menu/Details/5
        public ActionResult Details(int?page,int id)
        {
            ViewData["id"] = id;
            _re.Getdata(id).CountReader += 1;
            _re.Save();
            var item = _re.getComment(id);
            const int PageSize = 4;
            var ds = new PagetedList<Comment>(item, page ?? 0, PageSize);
            return View(ds);
        }
        [HttpPost]
        public ActionResult Details(int id, string Comment)
        {
            DataDataContext db=new DataDataContext();
            Comment item = new Comment();
            item.Body = Comment;
            item.UserId = re.GetDataByName(Session.GetPrincipal().Username).UserId;
            _re1.Add(item);
            _re1.Save();
            ArticleComment item1=new ArticleComment();
            item1.ArticleId = id;
            item1.CommentId = (from c in db.Comments
                                   where c.Body==Comment
                                   select c.CommentId).FirstOrDefault();
            _re2.Add(item1);
            _re2.Save();
            var x = _re.getComment(id);
            const int PageSize = 4;
            var ds = new PagetedList<Comment>(x,0, PageSize);            
            return RedirectToAction("Details", new {Controller="Menu",id,page=ds.TotalPages-1});
        }
        //
        // GET: /Menu/Create
        [GateAuthorize]
        [ValidateInput(false)]
        public ActionResult Create()
        {
            BindDdlCreatedById("-1");
            return View();
        }

        //
        // POST: /Menu1/Create
        [HttpPost]
        [GateAuthorize]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "Id")]Article ty)
        {
            try
            {
                // TODO: Add insert logic here
                if (Session.GetPrincipal().Username!="vuminhduan")
                {
                    ty.CatalogId = 4;
                }
                ty.DatetimeCreate = DateTime.Now;
                ty.DatetimeModify = DateTime.Now;
                ty.UserId =re.GetDataByName(Session.GetPrincipal().Username).UserId;
                re.GetDataByName(Session.GetPrincipal().Username).ArticleCount += 1;
                re.Save();
                _re.Add(ty);
                _re.Save();
                return RedirectToAction("Index","Home");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Menu1/Edit/5
        [GateAuthorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var item = _re.Getdata(id);
            return View(item);
        }

        //
        // POST: /Menu1/Edit/5
        [GateAuthorize(Roles = "admin")]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, Article Ty)
        {
            Article item = _re.Getdata(id);
            try
            {
                // TODO: Add update logic here
                item.DatetimeModify = DateTime.Now;
                item.TiTle= Ty.TiTle;
                item.Body = Ty.Body;
                _re.Save();
                return RedirectToAction("Details",new {id});
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Menu1/Delete/5       
        [GateAuthorize(Roles = "admin")]
        public ActionResult Delete(int id,string Delete)
        {
            _re.Delete(_re.Getdata(id));
            _re.Save();
            return RedirectToAction("Success");
        }
        public ActionResult Success()
        {
            return View();
        }
        private void BindDdlCreatedById(string createdById)
        {

            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "- Chon thu muc -", Value = "-1", Selected = "-1" == createdById.ToString() });
            list.Add(new SelectListItem { Text = "Máy Tính", Value = "1", Selected = "1" == createdById.ToString() });
            list.Add(new SelectListItem { Text = "Điện thoại", Value = "2", Selected = "2" == createdById.ToString() });
            list.Add(new SelectListItem { Text = "Góc Vui", Value = "3", Selected = "3" == createdById.ToString() });
            list.Add(new SelectListItem { Text = "Góc Tâm Sự", Value = "4", Selected = "4" == createdById.ToString() });
            ViewData["createdById"] = list;
        }
    }
}
